﻿using NCP.Model;
using NCP.Service;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebApp.Filters;
using NCP.Core.Extensions;
using NCP.Core.Exceptions;

namespace WebApp.Controllers
{
    [Authen]
    public class LimitDrugController : Controller
    {
        private LimitDrugService _service;

        public LimitDrugController(LimitDrugService service)
        {
            _service = service;
        }
        //科室限制
        public ActionResult Index()
        {
            return View();
        }
        //患者限制
        public ActionResult LimitPatient()
        {
            return View();
        }

        public ActionResult GetLimitDrugList()
        {
            Expressionable<PhaBaseInfo> exp = new Expressionable<PhaBaseInfo>();
            string drugCode = Request.Form["DrugCode"];
            string drugName = Request.Form["DrugName"];
            string validFlag = Request.Form["ValidFlag"];
            string setFlag = Request.Form["setFlag"];


            int pageIndex = Request.Form["page"].AsInt(1);
            int pageSize = Request.Form["limit"].AsInt(20);

            if (!string.IsNullOrEmpty(drugCode))
            {
                exp.And(x => SqlFunc.EndsWith(x.DrugCode, drugCode));
            }
            if (!string.IsNullOrEmpty(drugName))
            {
                drugName = drugName.ToUpper();
                exp.And(x => x.DrugName.Contains(drugName) || x.SpellCode.Contains(drugName));
            }
            if (validFlag != "ALL")
            {
                if (string.IsNullOrEmpty(validFlag)) validFlag = "1";
                exp.And(x => x.ValidFlag == validFlag);
            }
            if (!string.IsNullOrEmpty(setFlag) && setFlag != "ALL")
            {
                if (setFlag == "1")
                {
                    exp.And(x => SqlFunc.Subqueryable<LimitDrug>().Where(s => x.DrugCode == s.DrugCode).Any());
                }
                else
                {
                    exp.And(x => SqlFunc.Subqueryable<LimitDrug>().Where(s => x.DrugCode == s.DrugCode).NotAny());
                }
            }


            LayData ht = new LayData("0", "获取成功");
            try
            {
                PageModel pageModel = new PageModel() { PageIndex = pageIndex, PageSize = pageSize };
                ht.data = _service.GetLimitDrugList(exp, pageModel);
                ht.count = pageModel.TotalCount;
            }
            catch (Exception ex)
            {
                MessageException.CatchException(ex, ht);
            }

            return Content(ht.JsonToString(), "application/json");
        }

        public ActionResult GetLimitInfoList()
        {
            Expressionable<LimitDrug> exp = new Expressionable<LimitDrug>();
            string drug = Request.Form["DrugName"];
            string dept = Request.Form["DeptName"];
            string pat = Request.Form["Patient"];
            int pageIndex = Request.Form["page"].AsInt(1);
            int pageSize = Request.Form["limit"].AsInt(20);


            if (!string.IsNullOrEmpty(drug)) exp.And(x => SqlFunc.Subqueryable<PhaBaseInfo>().Where(b => b.DrugName.Contains(drug) && b.DrugCode == x.DrugCode).Any());
            if (!string.IsNullOrEmpty(dept)) exp.And(x => SqlFunc.Subqueryable<DeptInfo>().Where(d => d.DeptName.Contains(dept) && d.DeptCode == x.Code && x.Type == "1").Any());
            if (!string.IsNullOrEmpty(pat)) exp.And(x => SqlFunc.EndsWith(x.Code, pat) && x.Type == "2");

            LayData ht = new LayData("0", "获取成功");
            try
            {
                PageModel pageModel = new PageModel() { PageIndex = pageIndex, PageSize = pageSize };
                ht.data = _service.GetLimitInfoList(exp, pageModel);
                ht.count = pageModel.TotalCount;
            }
            catch (Exception ex)
            {
                MessageException.CatchException(ex, ht);
            }

            return Content(ht.JsonToString(), "application/json");
        }

        public ActionResult EditDept(string drugCode)
        {
            PhaBaseInfo item = _service.GetPhaBaseInfo(drugCode);
            return View(item);
        }

        public ActionResult EditPat(string drugCode)
        {
            PhaBaseInfo item = _service.GetPhaBaseInfo(drugCode);
            return View(item);
        }

        public ActionResult LimitDept1(string drugCode)
        {
            LayData ht = new LayData("0", "获取成功");
            try
            {
                ht.data = _service.LimitDeptUnChose(drugCode);
            }
            catch (Exception ex)
            {
                MessageException.CatchException(ex, ht);
            }

            return Content(ht.JsonToString(), "application/json");
        }

        public ActionResult LimitDept2(string drugCode)
        {
            LayData ht = new LayData("0", "获取成功");
            try
            {
                ht.data = _service.LimitDeptChose(drugCode);
            }
            catch (Exception ex)
            {
                MessageException.CatchException(ex, ht);
            }

            return Content(ht.JsonToString(), "application/json");
        }


        public ActionResult SaveLimitDept(List<LimitDrug> items, string drugCode)
        {
            LayData data = new LayData("0", "保存成功");
            try
            {
                _service.SaveLimitDrug(items, drugCode, "1");
            }
            catch (Exception ex)
            {
                MessageException.CatchException(ex, data);
            }

            return Json(data);
        }

        public ActionResult LimitPat1(string drugCode)
        {
            LayData ht = new LayData("0", "获取成功");
            try
            {
                ht.data = _service.LimitPatUnChose(drugCode);
            }
            catch (Exception ex)
            {
                MessageException.CatchException(ex, ht);
            }

            return Content(ht.JsonToString(), "application/json");
        }

        public ActionResult LimitPat2(string drugCode)
        {
            LayData ht = new LayData("0", "获取成功");
            try
            {
                ht.data = _service.LimitPatChose(drugCode);
            }
            catch (Exception ex)
            {
                MessageException.CatchException(ex, ht);
            }

            return Content(ht.JsonToString(), "application/json");
        }

        public ActionResult SaveLimitPat(List<LimitDrug> items, string drugCode)
        {
            LayData data = new LayData("0", "保存成功");
            try
            {
                _service.SaveLimitDrug(items, drugCode, "2");
            }
            catch (Exception ex)
            {
                MessageException.CatchException(ex, data);
            }

            return Json(data);
        }
    }
}